﻿/*
cd /projects/hsieh_project/proj_201809/code_1_data/
qstata --dofile=data_3_ind_sum_d_sel.do --statatype=mp --cpucount=5 &

Generates data set with
1) default weight for industry level analysis between year1 and year2
2) differences of selected variables between year1 and year2
*/

set linesize 255

clear all
cd /projects/

di "$S_TIME $S_DATE"

global rev_date: display %tdYYNNDD date("$S_DATE", "DMY")
di "${rev_date}"

global dir_proj "/projects/hsieh_project/proj_201809/"

global dir_data "${dir_proj}/data/"

global year1 = 1977
global year2 = 2013

global gl_perc = "10"
local l_perc = "10"

do "${dir_proj}/code_0_general/p_weight.do"

global ds_ind "${dir_data}/ind_sum_all_add"

//==============================================================================
// Average employment share (some sort of arch average)

capture program drop f_wperc
program f_wperc

args iperc
//local iperc = 10

use ${ds_ind}, clear
keep if inlist(year, ${year1}, ${year2})
merge 1:1 year ch_ind using ${dir_data}/sales_ind_sum_add
sort ch_ind year

//------------------------------------------------------------------------------
// Weight and change in emp share of top firms
p_weight ${year1} ${year2} "`iperc'" "0"

//------------------------------------------------------------------------------
// Change in absolute value or share of selected variables for subset of firms
foreach ivar in emp pay sales {
	// Top firms
	capture noi gen `ivar's_ind_`iperc' = `ivar'_ind_`iperc' / `ivar'_ind
	p_y1y2_d "`ivar's_ind" "`iperc'" "${year1}" "${year2}" "ln" "0" ""
	
	// Bottom firms
	capture noi gen `ivar'_ind_`iperc'b = (`ivar'_ind-`ivar'_ind_`iperc')
	p_y1y2_d "`ivar'_ind" "`iperc'b" "${year1}" "${year2}" "ln" "0" ""
	capture noi gen `ivar's_ind_`iperc'b = (`ivar'_ind-`ivar'_ind_`iperc') / `ivar'_ind
	p_y1y2_d "`ivar's_ind" "`iperc'b" "${year1}" "${year2}" "ln" "0" ""
	
	if `iperc' == 10 {
		
	foreach iiperc in 10 20 30 40 50 60 70 80 90 100 {
		// (iiperc-10) - iiperc % firms
		capture noi gen `ivar's_ind_`iiperc'd = (`ivar'_ind_`iiperc'-`ivar'_ind_`=`iiperc'-10') / `ivar'_ind // Emp should have been generated
		p_y1y2_d "`ivar's_ind" "`iiperc'd" "${year1}" "${year2}" "ln" "0" ""	
	}
	}
}

//------------------------------------------------------------------------------
// Change in absolute value of selected variables for all firms
local ivar = "emp"
p_y1y2_d "`ivar'_ind" "" "${year1}" "${year2}" "ln" "0" "0"
p_y1y2_d "`ivar'_ind" "" "${year1}" "${year2}" "" "0" "0"
foreach ivar in pay hhi sales sales_hhi {
p_y1y2_d "`ivar'_ind" "" "${year1}" "${year2}" "ln" "0" ""
p_y1y2_d "`ivar'_ind" "" "${year1}" "${year2}" "" "0" ""
}
// Average market size
foreach ivar in zipcode fips msa1983 msa1983cz {
p_y1y2_d "mkt_`ivar'" "" "${year1}" "${year2}" "ln" "0" ""
rename ln_mkt_`ivar'_d ln_mktsize_`ivar'_d
}

// Industry employment in two years
rename (emp_ind_y1 emp_ind_y2) (emp_ind_${year1} emp_ind_${year2})

// Export
if `iperc' == 10 local lvadd = "ln_*_ind_d_*d "
keep ch_ind w_emps_`iperc'* ///
	ln_emps_ind_d_`iperc'* ln_pays_ind_d_`iperc'* ///
	ln_emp_ind_d ln_pay_ind_d ln_hhi_ind_d hhi_ind_d ///
    emp_ind_${year1} emp_ind_${year2} ///
    ln_saless_ind_d_`iperc'* ln_saless_ind_d_`iperc'* ///
	ln_sales_ind_d ln_sales_hhi_ind_d sales_hhi_ind_d ///
	ln_*_ind_d_*b ln_mktsize_* `lvadd'

duplicates drop
save ${dir_data}/ind_sum_d_sel_`iperc'_${year1}_${year2}, replace

end

global year1 = 1977
global year2 = 2013

f_wperc 10
f_wperc 1

global year1 = 1977
global year2 = 2014

f_wperc 10
f_wperc 1

global year1 = 1977
global year2 = 2016

f_wperc 10
f_wperc 1

di "Ended at $S_DATE $S_TIME"
// End of do file
